package MTG::WizardsCom;
use Moose;

use Data::Dumper;
use strict;
use utf8;
use XML::Simple;


use base "MTG";

has 'cur_set' => ('is' => 'rw', default => sub { { } });


our @out;
sub run
{
	my ($self) = @_;
	
	foreach my $set (keys %{$self->config->{'sets'}})
	{	
		printf("Work With %s\n", $set);		
		$self->cur_set($self->config->{'sets'}->{$set});
		$self->parse();
	}
}


sub parse
{
	my ($self) = shift;
	
	my $cards;
	
	$self->get($self->cur_set->{'dbparse_wizards'});
	$self->clean_tree();
	$self->tree->parse($self->www->content());
	$self->tree->normalize_content();
	foreach my $table ($self->tree->find_by_tag_name("table"))
	{
		
		if ($table->attr("class") && $table->attr("class") eq "checklist")
		{
		
			foreach my $tr ($table->find_by_tag_name("tr"))
			{
				next if ($tr->attr("class") && $tr->attr("class") eq "headerRow");
				
				my $local_id = 0;
				my $id = ($tr->find_by_tag_name("td"))[0]->as_text();
				$local_id = $1 if ((($tr->find_by_tag_name("td"))[1]->find_by_tag_name("a"))[0]->attr('href') =~ /(\d+)$/);
				$cards->{$id}->{'local_id'} = $local_id;
				$cards->{$id}->{'code'} = $self->cur_set->{'code'};
			}
		}
	}
	
	foreach my $id (keys %$cards)
	{
		
		my $rating_url = sprintf($self->config->{'tech'}->{'dbparse_rating'}, $cards->{$id}->{'local_id'});
		my $legal_url = sprintf($self->config->{'tech'}->{'dbparse_legal'}, $cards->{$id}->{'local_id'});
		
		$self->get($rating_url);
		$self->clean_tree();
		$self->tree->parse($self->www->content());
		$self->tree->normalize_content();
		
		my $rating = ($self->tree->find_by_attribute("id", "ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_currentRating_textRatingContainer"))[0]->as_text();
	
		$rating = $1  if($rating =~ m#rating:\s*(.+?)\s*/\s*\d+#i);
		$cards->{$id}->{"rating"} = $rating;
		
		my $sth = $self->dbh->prepare("INSERT INTO rating VALUES (null,?, ?, ?, ?, ?)");
	  	$sth->execute(time(), 1, $id, $rating*1000, $rating_url);
		my $rule = $self->tree->find_by_attribute("id", "ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_rulingsContainer");
		my @rules;
		if ($rule)
		{
			$rule = ($rule)[0];
			
			foreach my $tr ($rule->find_by_tag_name("tr"))
			{
				my $date = ($tr->find_by_tag_name("td"))[0]->as_text();
				my $text = ($tr->find_by_tag_name("td"))[1]->as_text();

				my ($m, $d, $y) = split('/', $date);

				$date = sprintf("%.2d.%.2d.%.4d", $d, $m, $y);
				push @rules, {date => $date, text => $text};
			}
		}
	
		
		$cards->{$id}->{'rules'} = \@rules if (scalar(@rules) > 0);
		
		
		$self->get($legal_url);
	
		$self->clean_tree();
		$self->tree->parse($self->www->content());
		$self->tree->normalize_content();
		
		my $l_table = ($self->tree->find_by_attribute("class", "cardList"))[1];
		
		my @legal;
		
		if ($l_table)
		{
			foreach my $tr ($l_table->find_by_tag_name("tr"))
			{
				next if ($tr->attr("class") && $tr->attr("class") eq "headerRow");
				
				my $format = ($tr->find_by_tag_name("td"))[0];
				
				if ($format)
				{
					$format = $format->as_text();
				}
				else
				{
					next;
				}
				
				$format =~ s/^\s*//;
				$format =~ s/\s*$//;
				
				my $legal = ($tr->find_by_tag_name("td"))[1];
				if ($legal)
				{
					$legal = $legal->as_text();
				}
				else
				{
					next;
				}
				
				$legal =~ s/^\s*//;
				$legal =~ s/\s*$//;
				
				if ($legal =~ /^legal$/io)
				{
					$legal = 1;
				}
				else
				{
					$legal = 0;
				}
				
				push @legal, { format => $format, legal => $legal};
				
			}
		}
		
		
		
		
		$cards->{$id}->{'legal'} = \@legal if (scalar(@legal) > 0);
		
		foreach my $l (@legal)
		{
#			( id INTEGER PRIMARY KEY, last_update int, card_code_id int, card_id int, format varchar(255), status int, update_link TEXT);
			my $sth = $self->dbh->prepare("INSERT INTO `formats` VALUES (null,?, ?, ?, ?, ?, ?)");
			$sth->execute(time(), $self->cur_set->{'code_id'},,$id, $l->{'format'}, $l->{'legal'},$legal_url );	
		}
		
	
	}

	
}


1;